<?php
namespace app\home\controller;
use app\home\model\UserM;
use app\home\model\DepartM;
use think\Controller;
use think\Db;
use myResponse\Res;
use think\Session;

class Info  extends Controller
{

    public function userAll()
    {
        $res =Db::name('sys_user')->select();
        Res::suc('ok',$res);
    }
    /** 部门 */
    public function departAll()
    {
        $sup = new DepartM();
        $res =$sup->all();
//        $res =Db::name('sys_department')->select();
        Res::suc('ok',$res);
    }

    public function rolesAll()
    {
        $res =Db::name('sys_role')->select();
        Res::ok($res);
    }
    /**
     * 点检
     */

    //点检标准类型
    public function checkStandardType()
    {
        $res =Db::name('checkStandardType')->order('CreateTime desc')->select();
        Res::ok($res);
    }
    //点检标准类型 点检标准分类
    public function checkStandardCategory()
    {
        $res =Db::name('checkStandard')->order('CreateTime desc')->select();
        Res::ok($res);
    }
    //点检标准类型 点检周期
    public function choseCycleType()
    {
        $res =Db::name('check_cycle_type')->order('CreateTime desc')->select();
        Res::ok($res);
    }
    //巡检路线
    public function patrolLine()
    {

        $res =Db::name('patrol_line')->order('CreateTime desc')->select();
        Res::ok($res);
    }
    /**
     * 设备
     */
    // 设备安装位置
    public function devicePosition()
    {
        $res =Db::name('sb')->distinct(true)->field('azwz')->select();
        Res::suc('ok',$res);
    }
    // 设备分类
    public function deviceCategory()
    {
        $res =Db::name('sbfl')->select();
        $list= $this->getTree($res);
//        $list2= $this->GetTreeList($res);     df($list2);die;
//        df($list);die;
        Res::suc('ok',$list);
    }

    // 设备id ->备件
    public function deviceParts()
    {
        $res =Db::name('sbbj')->where('sbid='.input('id'))->select();
        Res::suc('ok',$res);
    }

    /** tree目录结构
     * @param $arr
     * @param int $pid
     * @param int $level
     * @return array
     */
    function getTree($arr,$pid=0,$level=0)
    {
        $list =array();
        foreach ($arr as $k=>$v){
            if ($v['fl_upid'] == $pid){
                $v['level']=$level;
                $child=$this->getTree($arr,$v['fl_id'],$level+1);
                if(count($child)){
                    $v['leaf'] = false; //是否子节点
                    $v['child'] = $child;
                }else{
                    $v['leaf'] = true;
                }
                $list[] = $v;
            }
        }
        return $list;
    }
    // ──a ───aa
    // ──b ───bb
    private function GetTreeList($arr, $pid=0, $level=0)
    {
        global $tree;
        foreach ($arr as $key => $val) {
            if ($val['fl_upid'] == $pid) {
                $flg = str_repeat('─', $level); // →
                $val['fl_mc'] = $flg . $val['fl_mc'];
                $tree[] = $val;
                $this->GetTreeList($arr, $val['fl_id'], $level + 1);
            }
        }
        return $tree;
    }
}
